//
//  YZMStoptackTableViewCell.m
//  YZMicroStock
//
//  Created by apple on 16/11/3.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStoptackTableViewCell.h"
#import <ColorUtils/ColorUtils.h>
#import "NSNumber+Utils.h"
#import "YZMConfigureService.h"
#import <DateTools/DateTools.h>
#import "YZMSeverTimeService.h"
#import "YZMSaleTradeModel.h"
#import "YZMAlertView.h"
#import <BlocksKit/UIControl+BlocksKit.h>

@interface YZMStoptackTableViewCell ()
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *numberLabel;
@property(nonatomic, strong) UILabel *daytimeLabel;
@property(nonatomic, strong) UIButton *saleButton;
@property(nonatomic, strong) UIView *line1;
@property(nonatomic, strong) UIView *line2;
@property(nonatomic, strong) UILabel *currentNumber;
@property(nonatomic, strong) UILabel *grabNumber;
@property(nonatomic, strong) UILabel *saleNumber;
@property(nonatomic, strong) UILabel *positionNumber;
@property(nonatomic, strong) UIImageView *arrawImageView;
@property(nonatomic, strong) UILabel *salingLabel;
@property(nonatomic, strong) UILabel *currentLabel;
@property (nonatomic, strong) YZMConfigureService *service;
@property (nonatomic, strong) UIButton *apartBT;
@property(nonatomic, strong) UILabel *positionLabel;


@end

@implementation YZMStoptackTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createObject];
        self.backgroundColor = [YZMAppStyle colorForMain];
    }
    return self;
}

-(void)createObject
{
    self.nameLabel=[YZMAppStyle labelWithLargeWhite];
    [self addSubview:_nameLabel];
    _nameLabel.font=[UIFont systemFontOfSize:16];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.mas_equalTo(self).offset(12);
 //       make.width.mas_equalTo(64);
//        make.height.mas_equalTo(16);
    }];
    
    self.numberLabel=[YZMAppStyle labelWithMediumGray];
    [self addSubview:_numberLabel];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLabel);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(6);
//        make.width.mas_equalTo(80);
//        make.height.mas_equalTo(14);
    }];
    
    self.daytimeLabel=[YZMAppStyle labelWithBuyWay];
    [self addSubview:_daytimeLabel];
    _daytimeLabel.layer.cornerRadius=3;
    _daytimeLabel.textColor=[UIColor colorWithString:@"#F5C300"];
    _daytimeLabel.layer.borderColor=[UIColor colorWithString:@"#F5C300"].CGColor;
    _daytimeLabel.font=[UIFont systemFontOfSize:9];
    _daytimeLabel.hidden=YES;
    [_daytimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_right).offset(5);
        make.centerY.equalTo(_nameLabel);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(16);
    }];
    
    self.saleButton=[YZMAppStyle regButton];
    [self addSubview:_saleButton];
    [_saleButton setTitle:@"卖出" forState:UIControlStateNormal];
    [_saleButton setTitleColor:[YZMAppStyle colorForMain] forState:UIControlStateNormal];
    _saleButton.backgroundColor=[YZMAppStyle colorForYellowButton];
    
    [self.saleButton setBackgroundImage:[UIImage createImageWithColor:[YZMAppStyle colorForGrayUnable]] forState:UIControlStateDisabled];
    _saleButton.titleLabel.font=[YZMAppStyle mediumSizeFont];
    _saleButton.layer.borderWidth=0;
    [_saleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.top.mas_equalTo(self).offset(12);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    _saleButton.hidden=YES;
    [_saleButton addTarget:self action:@selector(seleaction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.line1=[YZMAppStyle splitLine];
    [self addSubview:_line1];
    [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.mas_equalTo(_numberLabel.mas_bottom).offset(12);
        make.width.mas_equalTo(ScreenWidth-30);
    }];
    
    self.salingLabel=[YZMAppStyle labelWithMediumWhite];
    self.salingLabel.textColor=[YZMAppStyle colorForYellowButton];
    [self addSubview:_salingLabel];
    _salingLabel.hidden=YES;
    [_salingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameLabel);
        make.right.mas_equalTo(_line1.mas_right);
    }];
    
    
    self.arrawImageView=[[UIImageView alloc]init];
    [self.arrawImageView setImage:[UIImage imageNamed:@"historyarrow"]];
    [self addSubview:_arrawImageView];
    [_arrawImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.top.mas_equalTo(self).offset(23);
        make.height.mas_equalTo(13);
        make.width.mas_equalTo(7);
    }];
    _arrawImageView.hidden=YES;
    
    self.line2=[YZMAppStyle splitLine];
    [self addSubview:_line2];
    [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.width.mas_equalTo(_line1);
    }];
    
    _currentLabel=[YZMAppStyle labelWithMediumGray];
    [self addSubview:_currentLabel];
    _currentLabel.text=@"当前价格";
    [_currentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.mas_equalTo(_line1).offset(9);
        make.width.mas_equalTo(ScreenWidth/4-15);
        make.height.mas_equalTo(14);
    }];
    
    self.currentNumber=[YZMAppStyle labelWithMediumWhite];
    [self addSubview:_currentNumber];
    [_currentNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_currentLabel);
        make.top.mas_equalTo(_currentLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(_currentLabel);
        make.height.mas_equalTo(14);
    }];
    
    UILabel *grabLabel=[YZMAppStyle labelWithMediumGray];
    [self addSubview:grabLabel];
    grabLabel.text=@"持仓市值";
    [grabLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_currentLabel.mas_right).offset(15);
        make.top.mas_equalTo(_currentLabel);
        make.width.mas_equalTo(ScreenWidth/4-10);
        make.height.mas_equalTo(14);
    }];
    
    self.grabNumber=[YZMAppStyle labelWithMediumWhite];
    [self addSubview:_grabNumber];
    [_grabNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_currentLabel.mas_right).offset(15);
        make.centerY.mas_equalTo(_currentNumber);
        make.width.mas_equalTo(ScreenWidth/4-10);
        make.height.mas_equalTo(14);
    }];
    
    UILabel *saleNumberLabel=[YZMAppStyle labelWithMediumGray];
    [self addSubview:saleNumberLabel];
    saleNumberLabel.textAlignment=NSTextAlignmentLeft;
    saleNumberLabel.text=@"可卖数量";
    [saleNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(grabLabel.mas_right).offset(15);
        make.top.mas_equalTo(_currentLabel);
        make.width.mas_equalTo(ScreenWidth/4);
        make.height.mas_equalTo(14);
    }];
    
    self.saleNumber=[YZMAppStyle labelWithMediumWhite];
    [self addSubview:_saleNumber];
    _saleNumber.textAlignment=NSTextAlignmentLeft;
    [_saleNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(grabLabel.mas_right).offset(15);
        make.centerY.mas_equalTo(_grabNumber);
        make.width.mas_equalTo(ScreenWidth/4);
        make.height.mas_equalTo(14);
    }];
    
    _positionLabel=[YZMAppStyle labelWithMediumGray];
    [self addSubview:_positionLabel];
    _positionLabel.text=@"持仓数量";
    _positionLabel.textAlignment=NSTextAlignmentRight;
    [_positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-33);
        make.top.mas_equalTo(_currentLabel);
        make.width.mas_equalTo(ScreenWidth/4);
        make.height.mas_equalTo(14);
    }];
    
    self.apartBT = [YZMAppStyle explainButton];
    self.apartBT.hidden = YES;
    [self.apartBT bk_addEventHandler:^(id sender) {
        
        if (_model.tradingList.count != 0) {
            [[self alertViewForApartSellWithList:_model.tradingList andTotalAmount:_model.holdCount+_model.apartCount   andApartAmount:_model.holdCount] showInView:YZMHCMInstance.currentNavationCtroller.view];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.apartBT];
    [self.apartBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_positionLabel);
        make.left.equalTo(_positionLabel.mas_right);
    }];
    
    
    self.positionNumber=[YZMAppStyle labelWithMediumWhite];
    [self addSubview:_positionNumber];
    _positionNumber.textAlignment=NSTextAlignmentRight;
    [_positionNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-13);
        make.centerY.mas_equalTo(_saleNumber);
        make.width.mas_equalTo(ScreenWidth/4);
        make.height.mas_equalTo(14);
    }];
}


-(void)setvaluewithsModel:(YZMSaleTradeModel *)sModel withpModel:(YZMStockRealModel *)pModel
{
    _model=sModel;
    _nameLabel.text=sModel.stockName;
    _numberLabel.text=sModel.stockCode;
    _positionNumber.text=[NSString stringWithFormat:@"%@",@(sModel.holdCount)];
    _saleNumber.text=[NSString stringWithFormat:@"%@",@(sModel.holdCount)];
    _salingLabel.hidden=NO;
    _saleButton.hidden=YES;
    _daytimeLabel.hidden=YES;
    _arrawImageView.hidden=YES;
    if (sModel.schemeId>0) {
        _service = [YZMConfigureService sharedServiceWithTradeType:YZM_TRADE_TYPE_FOR_A_STOCK_D];
    }else{
        _service = [YZMConfigureService sharedServiceWithTradeType:YZM_TRADE_TYPE_FOR_A_STOCK];
    }
    
    if (sModel.tradingList.count>0) {
        _apartBT.hidden=NO;
        [_positionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-33);
            make.top.mas_equalTo(_currentLabel);
            make.width.mas_equalTo(ScreenWidth/4);
            make.height.mas_equalTo(14);
        }];
    }else{
        _apartBT.hidden=YES;
        [_positionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-13);
            make.top.mas_equalTo(_currentLabel);
            make.width.mas_equalTo(ScreenWidth/4);
            make.height.mas_equalTo(14);
        }];
    }
    if (pModel) {

        _currentNumber.text=[NSString stringWithFormat:@"%@",@(pModel.price).p02fString];
        _grabNumber.text=[NSString stringWithFormat:@"%@",@(pModel.price*sModel.holdCount).p02fString];
    
        if (pModel.isSuspended) {
            _daytimeLabel.text=@"已停牌";
            _daytimeLabel.hidden=NO;
            _currentNumber.text=[NSString stringWithFormat:@"%@",@(pModel.lastClosePrice).p02fString];
            _grabNumber.text=[NSString stringWithFormat:@"%@",@(pModel.lastClosePrice*sModel.holdCount).p02fString];
            _saleNumber.text=@"0";
        }
        
        switch (sModel.status) {
//            case YZMSaleTradeStausForMatched:
//                if ([_service isInTradeTimePeriods]) {
//                    _salingLabel.textColor=[YZMAppStyle colorForYellowButton];
//                    _salingLabel.text=@"正在委托买入";
//                }else{
//                    _salingLabel.textColor=[YZMAppStyle colorForGrayText];
//                    _salingLabel.text=@"非交易时间段";
//                }
//                break;
//                
//            case YZMSaleTradeStausForTrading:
//                if ([_service isInTradeTimePeriods]) {
//                    _salingLabel.textColor=[YZMAppStyle colorForYellowButton];
//                    _salingLabel.text=@"买入";
//                }else{
//                    _salingLabel.textColor=[YZMAppStyle colorForGrayText];
//                    _salingLabel.text=@"非交易时间段";
//                }
//                break;
//                
            case YZMSaleTradeStausForHolding:{
                _salingLabel.hidden=YES;
                if (!pModel.isSuspended) {
                    if ([_service isInTradeTimePeriods]) {
                        if ([sModel.dealTime isSameDay:[YZMSeverTimeService sharedService].severDate]) {
                            _saleButton.hidden=NO;
                            _saleButton.enabled=NO;
                        }else{
                            _saleButton.hidden=NO;
                            _saleButton.enabled=YES;
                        }
                    }else{
                        _salingLabel.hidden=NO;
                        _salingLabel.textColor=[YZMAppStyle colorForGrayText];
                        _salingLabel.text=@"非交易时间段";
                    }
                }
            }
                break;
                
            case YZMSaleTradeStausForToClose:
            case YZMSaleTradeStausForClosing:
                if ([_service isInTradeTimePeriods]) {
                    _salingLabel.textColor=[YZMAppStyle colorForYellowButton];
                    _salingLabel.text=@"正在委托卖出";
                }else{
                    _salingLabel.textColor=[YZMAppStyle colorForGrayText];
                    _salingLabel.text=@"非交易时间段";
                }
                break;
                
            case YZMSaleTradeStausForDating:
                if ([_service isInTradeTimePeriods]) {
                    _salingLabel.textColor=[YZMAppStyle colorForYellowButton];
                    _salingLabel.text=@"正在清算";
                }else{
                    _salingLabel.textColor=[YZMAppStyle colorForGrayText];
                    _salingLabel.text=@"非交易时间段";
                }
                break;
                
            case YZMSaleTradeStausForDated:
                _currentLabel.text=@"卖出价格";
                _salingLabel.hidden=YES;
                _daytimeLabel.text=@"已结算";
                _daytimeLabel.hidden=NO;
                _arrawImageView.hidden=NO;
                _currentNumber.text=[NSString stringWithFormat:@"%@",@(sModel.sellPriceDeal).p02fString];
                _grabNumber.text=@"--";
                _saleNumber.text=@"0";
                _positionNumber.text=@"0";
                break;
                
            default:
                break;
        }
    }else{
        _saleButton.hidden=YES;
        _salingLabel.hidden=NO;
        _daytimeLabel.hidden=NO;
        _grabNumber.text=@"--";
        if (sModel.status!=8) {
            _salingLabel.textColor=[YZMAppStyle colorForGrayText];
            _salingLabel.text=@"非交易时间段";
            _daytimeLabel.text=@"已停牌";
            _currentNumber.text=@"--";
        }else{
            _salingLabel.hidden=YES;
            _daytimeLabel.text=@"已结算";
            _arrawImageView.hidden=NO;
        }
        _saleNumber.text=@"0";
    }
}

-(void)setIslastcell:(BOOL)islastcell
{
    if (islastcell) {
        _line2.hidden=YES;
    }
}


-(void)seleaction:(UIButton *)button
{
    if (self.saleblock) {
        self.saleblock();

    }
}

- (YZMAlertView *)alertViewForApartSellWithList:(NSArray *)apartList andTotalAmount:(NSInteger)totalAmount andApartAmount:(NSInteger)apartAmount{
    
    YZMAlertView *alertView = [YZMAlertView new];
    alertView.contentLabel.text = [NSString stringWithFormat:@"行情波动过大，造成部分成交，原持有数量%@股，现剩余%@股，请处理剩余数量，以便结算！",@(totalAmount), @(apartAmount)];
    alertView.contentLabel.textColor = [YZMAppStyle colorForBlackText];
    alertView.contentLabel.font = [YZMAppStyle mediumSizeBoldFont];
    alertView.cancelButton.hidden = YES;
    
    UIView *stackView = [UIView new];
    
    NSMutableArray *leftArray = [NSMutableArray arrayWithObjects:@"成交时间", nil];
    NSMutableArray *middleArray = [NSMutableArray arrayWithObjects:@"数量", nil];
    NSMutableArray *rightArray = [NSMutableArray arrayWithObjects:@"价格", nil];
    
    for (YZMSaleTradeModelForApart *apart in apartList) {
        
        [leftArray addObject:[apart.time formattedDateWithFormat:@"YYYY-MM-dd"]];
        [middleArray addObject:[NSString stringWithFormat:@"%@",@(apart.amount)]];
        [rightArray addObject:@(apart.price).p02fString];
    }
    
    NSArray *array = @[leftArray, middleArray, rightArray];
    [array enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {
        
        UIView *subStackView = [UIView new];
        [stackView addSubview:subStackView];
        [subStackView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(stackView);
            make.top.equalTo(stackView).offset(10);
            if (idx == 0) {
                make.left.equalTo(stackView);
            }else if(idx == 1){
                
                make.centerX.equalTo(stackView).offset(20);
                
            }else{
                
                make.right.equalTo(stackView);
            }
        }];
        
        
        UIView *lastView = nil;
        BOOL flag = YES;
        for (NSString *string in obj) {
            UILabel *label = [YZMAppStyle labelWithMediumGray];
            label.text = string;
            
            [subStackView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                
                if (idx == 0) {
                    
                    make.left.equalTo(subStackView);
                    make.right.lessThanOrEqualTo(subStackView);
                    
                }else{
                    
                    make.left.greaterThanOrEqualTo(subStackView);
                    make.right.equalTo(subStackView);
                }
                
                if (lastView) {
                    
                    make.top.equalTo(lastView.mas_bottom).offset(flag?8:2);
                    
                }else{
                    make.top.equalTo(subStackView);
                }
            }];
            
            if (lastView) {
                flag = NO;
            }
            lastView = label;
            
        }
        
        if (idx == 0) {
            
            [subStackView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.equalTo(lastView);
            }];
        }
        
    }];
    
    
    UIView *splitLine = [YZMAppStyle splitLine];
    [stackView addSubview:splitLine];
    [splitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView).offset(-2);
        make.left.equalTo(stackView).offset(-4);
        make.right.equalTo(stackView).offset(4);
    }];
    
    alertView.customView = stackView;
    
    return alertView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
